// 配置 APPServer 服务域名
export const ServicesOrigin = 'https://zyqtest.tjhaiqing.com/';//测试
// export const ServicesOrigin = 'https://zyq.tjhaiqing.com/';//正式
//'http://livedemo.apsaralive-github-test.1428367983895556.cn-shanghai.fc.devsapp.net'
//'http://live-example.live.1569899459811379.cn-hangzhou.fc.devsapp.net';

// 配置api接口路径前缀
// export const ApiPrefixPath = '/index.php';

// const UserInfoStorageKey = 'user_info';
const userId = '';
const userName = '';
const authToken = '';
const expire = '';
class Services {
  constructor() {


    // 从缓存中读用户数据
    try {
      const infoStr = wx.getStorageSync(UserInfoStorageKey);
      console.log(infoStr, 99987)
      if (infoStr) {
        const info = JSON.parse(infoStr);
        userId = info.userId;
        userName = info.userName;
        authToken = info.authToken;
        expire = info.expire;
      }
    } catch (e) {
      //TODO handle the exception
    }
  }

  getUserInfo() {
    return {
      userId: wx.getStorageSync('user_id'),
      userName: wx.getStorageSync('username'),
    };
  }

  request(api, data, header) {
    const url = ServicesOrigin + 'lives/api.lives/visit_liveroom';
    // console.log('url-->', url);
    return new Promise((resolve, reject) => {
      wx.request({
        url,
        method: 'POST',
        data,
        header,
        success(res) {
          if (res.statusCode === 200 && res.data) {
            resolve(res.data);
            return;
          }
          if (res.statusCode === 401) {
            // 处理登录态失效等问题
          }
          reject(res.data);
        },
        fail(err) {
          reject(err);
        }
      })
    });
  }

  login(userId, username) {
    // 实际场景请勿明文传密码
    return this.request(
      'login', {
        username,
        password: username,
      },
    ).then((res) => {
      this.userId = userId;
      this.userName = username;
      this.authToken = res.token;
      this.expire = res.expire;

      uni.setStorage({
        key: UserInfoStorageKey,
        data: JSON.stringify({
          userId,
          userName: username,
          authToken: res.token,
          expire: res.expire,
        }),
      });
    });
  }

  getRoomList(pageNum, pageSize) {
    return this.request(
      'list', {
        user_id: this.userId,
        page_num: pageNum,
        page_size: pageSize,
      }, {
        Authorization: `Bearer ${this.authToken}`,
      }
    );
  }

  getRoomDetail(roomId,userid) {
    console.log(roomId)
    return this.request(
      'post', {
        s: 'lives/api.lives/visit_liveroom',
        user_id:userid,
        login_token:wx.getStorageSync('login_token'),
        aly_live_id: roomId,
      }, {
        Authorization: `Bearer ${this.authToken}`,
      }
    );
  }

  getToken() {
    return this.request(
      'token', {
        user_id: this.userId,
        device_type: 'web',
        device_id: 'isuniapp',
      }, {
        Authorization: `Bearer ${this.authToken}`,
      }
    );
  }
}

export default new Services();